class UserStory < ActiveRecord::Base
  after_save      :log_history
  attr_accessible :id, :answers_to_questionnaires_attributes, :answers_to_questionnaires, :criterions, :criterions_attributes, :criterions_id, :description, :headline, :verb, :noun, :answers_to_questionnaire_id, :product_backlog_pid, :user_pid, :published, :user_story_state_pid, :user_story_priority_pid, :sprint_backlog_id, :user_story_rating_id, :sprint_backlog_column_id, :created_at, :updated_at

  belongs_to :product_backlog
  belongs_to :sprint_backlog
  belongs_to :user_story_priority
  belongs_to :user_story_state
  belongs_to :user_story_rating
  
  has_many :answers_to_questionnaires
  has_many :criterions
  
  validates :headline, :presence => true
  validates :description, :presence => true
  validates :user_story_state_pid, :presence => true
  validates :user_story_priority_pid, :presence => true
  
  accepts_nested_attributes_for :answers_to_questionnaires
  accepts_nested_attributes_for :criterions
  
  def log_history
    UserstoryHistory.create(:userstory_id => self.id, :userstorystate_id => self.user_story_state_pid, :user_story_rating_id => self.user_story_rating_id, :date => DateTime.current())
  end
end
